package com.diqye.controller.blog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.diqye.controller.blog.table.active_user_tab;
import com.diqye.controller.blog.table.blog_tab;
import com.diqye.controller.blog.table.review_tab;
import com.diqye.controller.util.CKit;
import com.diqye.db.Tinfo;
import com.diqye.db.model.XModel;
import com.diqye.engine.DBController;
import com.diqye.engine.render.Error404Render;
import com.diqye.engine.render.JspRender;
import com.diqye.templet.util.Strings;

public class Blog extends DBController {
	public Class<? extends Tinfo> tab=blog_tab.class;
	public void save(){
		Object login=CKit.getLogin(getRequest());
		if(login==null){
			renderErr("你没有登录 ");
			return;
		}
		getParams().s("author", ((XModel)login).gts("qq"));
		getParams().s("createdate",new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
		getParams().setTinfo(tab);
		db.save(getParams());
		renderOk(getParams().gts("id"));
	}
	
	public void view(){
		String id=request.getParameter("id");
		if(id==null || id.matches("^[^0-9]+$")){
			render(new Error404Render());
			return;
		}
		Integer id1=Integer.valueOf(id);
		String sql ="SELECT t1.id,t1.author,t1.title,t1.tag,content,t1.createdate,t2.sname,t2.name FROM blog t1 INNER JOIN user_tab t2 ON t1.author=t2.qq and t1.id=#{0}";
		XModel model1=db.findFirst(sql, id1);
		if(model1==null){
			render(new Error404Render());
			return;
		}
		render(new JspRender("WEB-INF/jsp/view.jsp",model1));
	}
	
	public void addReview(){
		String review;
		if(CKit.isLogin(getRequest())){
			review=CKit.getLoginSname(getRequest());
		}else{
			review = getRequest().getRemoteAddr();
		}
		getParams().setTinfo(review_tab.class)
		.s("review", review)
		.s("reviewdate", CKit.getCurrentDate());
		db.save(getParams());
		renderOk(1);
	}
	public void queryReview(){
		String sql="SELECT id,target,review, content,reviewdate FROM review_tab where target=#{0} ORDER BY id DESC limit #{1},#{2} ",
				sql2="SELECT count(id) FROM review_tab where target=#{0}";
		long count = db.findLong(sql2, getParams().gts("target"),getParams().getInt("currentIndex"),getParams().getInt("pageSize"));
		List list = db.find(sql, getParams().gts("target"),getParams().getInt("currentIndex"),getParams().getInt("pageSize"));
		renderOk(new XModel().s("count", count).s("list", list));
	}
	
	public void queryOne(){
		String sql="select * from blog where id=#{0}";
		XModel model=db.findFirst(sql, getParams().getInt("id"));
		if(model==null)renderErr("该博文已不存在请刷新后在操作");
		renderOk(model);
	}
	public void queryLi(){
		String 
				time=getParams().gts("time"),
				title=getParams().gts("title"),
				tag=getParams().gts("tag"),
				sql=" select id,title,tag,createdate from blog ",
				sql2=" select count(*) from blog ",
				where=" where author='"+CKit.getLoginQQ(getRequest())+"' ",
				dir=" order by id desc ",
				limit=" limit #{0},#{1} ";
		
		if(CKit.notEmpty(time)){
			if(time.equals("current_month")){
				where=CKit.and(where,"createdate >=#{2}");
			}
			if(time.equals("last_month")){
				where=CKit.and(where,"createdate between #{3} and #{2}");
			}
		}
		
		if(CKit.notEmpty(title)){
			where =CKit.and(where,"title like '%${4}%'");
		}
		
		if(CKit.notEmpty(tag)){
			where =CKit.and(where,"tag = #{5}");
		}
		
		long count=db.findLong(sql2+where
				, getParams().getInt("currentIndex")
				, getParams().getInt("pageSize")
				,CKit.getCurrentMonth()
				,CKit.getLastMonth()
				,title
				,tag
				);
		List<XModel> list = db.find(sql+where+dir+limit
				, getParams().getInt("currentIndex")
				, getParams().getInt("pageSize")
				,CKit.getCurrentMonth()
				,CKit.getLastMonth()
				,title
				,tag
				);
		
		renderOk(CKit.list(count, list));
	}
	
	public void updateLi(){
		getParams().setTinfo(tab);
		db.update(getParams());
		renderOk(null);
	}
	
	public void deleteTgLi(){
		String sql="update blog_tab set isdel = '1' where id in(${0})";
		List ids = getParams().getT("ids");
		renderOk(db.update(sql, Strings.join(ids, ",")));
		
	}
	
	public void newest(){
		String sql="SELECT t1.id,t1.title,t2.sname,t2.name,t1.createdate FROM blog t1 INNER JOIN user_tab t2 ON t1.author=t2.qq ORDER BY id DESC LIMIT #{0},#{1}";
		renderOk(db.find(sql, getParams().getInt("currentIndex"), getParams().getInt("pageSize")));
	}
	
	public void joinActiveLi(){
		XModel model=new XModel();
		model.setTinfo(active_user_tab.class);
		model.set("activeid", getParams().getInt("activeid"));
		model.set("userid", CKit.getLoginQQ(getRequest()));
		model.set("time", CKit.getCurrentDate());
		model.set("note",Strings.format("通过({0})加入", getParams().gts("blogid")));
		db.save(model);
		renderOk(1);//加入成功
	}
	
	public void isJoinActive(){
		if(!CKit.isLogin(getRequest())){
			renderOk(2); //未登录
			return;
		}
		String qq=CKit.getLoginQQ(getRequest()),
			   sql="SELECT COUNT(*) FROM active_user_tab WHERE activeid=#{0} AND userid = #{1}";
		long l=db.findLong(sql, getParams().getInt("activeid"),qq);
		if(l>0)renderOk(1);//已经加入
		else renderOk(7);//未加入
	}
	
	public void joinCount(){
		String sql="select count(*) from active_user_tab  WHERE activeid=#{0}";
		long l=db.findLong(sql, getParams().getInt("activeid"));
		renderOk(l);
	}
}
